//import 'package:tripledes/tripledes.dart';
import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:wgq_dart/src/comm_function/core.dart';
import 'dart:typed_data';
import 'package:uuid/uuid.dart';

//import 'package:encrypt/encrypt.dart';
//import 'package:flutter_des/flutter_des.dart';
import 'package:dart_des/dart_des.dart';
import 'package:bcrypt/bcrypt.dart';

class WDEncrypt{

	static String obtainAHash(){
		return Uuid().v1();
	}
	
	//region 对称加密
	/// Aes加解密
//	static String encryptUseAes(String originText,String keyStr) {
//		final key = Key.fromUtf8(keyStr);
//		final iv = IV.fromLength(16);
//		final encrypter = Encrypter(AES(key, iv,mode: AESMode.ecb));
//
//		// encrypted是加密后的Encrypted对象,并不是String,需要用base64方法获取字符串
//		final encrypted = encrypter.encrypt(originText);
//		return encrypted.base64;
//	}
//
//	static String decryptUseAes(String encryptedStr,String keyStr){
//		final key = Key.fromUtf8(keyStr);
//		final iv = IV.fromLength(16);
//		final encrypter = Encrypter(AES(key, iv,mode: AESMode.ecb));
//
//		// 解密的方法需要一个Encrypted对象,所以需要通过密文创建一个Encrypted对象
//		// 解密出来的直接就是字符串
//		final encrypted = Encrypted.fromBase64(encryptedStr);
//		return encrypter.decrypt(encrypted);
//	}
//
//	/// 3des加解密(注意:对中文支持有点问题)
//	static String encryptUse3des(String originText,String keyStr) {
//		var blockCipher = new BlockCipher(new DESEngine(), keyStr);
//		return blockCipher.encodeB64(originText);
//	}
//
//	static String decryptUse3des(String encryptedStr,String keyStr){
//		var blockCipher = new BlockCipher(new DESEngine(), keyStr);
//		return blockCipher.decodeB64(encryptedStr);
//	}

	/// des加解密
	static String encodeUseDES(String originText,String key){
		final finalKey = key.forceLength(8);
		List<int> encrypted;
		DES desECBPKCS5 = DES(
			key: finalKey.codeUnits,
			mode: DESMode.ECB,
			paddingType: DESPaddingType.PKCS5,
		);
		encrypted = desECBPKCS5.encrypt(originText.codeUnits);
		print('list1:$encrypted');
		return base64.encode(encrypted);
	}

	static String decodeUseDES(String cipherText,String key){
		final finalKey = key.forceLength(8);
		DES desECBPKCS5 = DES(
			key: finalKey.codeUnits,
			mode: DESMode.ECB,
			paddingType: DESPaddingType.PKCS5,
		);
		final List<int> originCipherText = base64.decode(cipherText);
		print('list2:$originCipherText');
		final originText = desECBPKCS5.decrypt(originCipherText);
		return utf8.decode(originText);
	}

	/// 3des加解密
	static String encodeUse3Des(String originText,String key,{
		List<int>? iv =const [4,3,2,1,8,5,6,7]
	}){
		List<int> encrypted;
		DES3 des3CBC = DES3(key: key.codeUnits, mode: DESMode.CBC, iv: iv!);
		encrypted = des3CBC.encrypt(originText.codeUnits);
		return base64.encode(encrypted);
	}

	static String decodeUse3Des(String cipherText,String key,{
		List<int>? iv =const [4,3,2,1,8,5,6,7]
	}){
		List<int> decrypted;
		DES3 des3CBC = DES3(key: key.codeUnits, mode: DESMode.CBC, iv: iv!);
		decrypted = des3CBC.decrypt(base64.decode(cipherText));
		return utf8.decode(decrypted);
	}


	/// base64加解密
	static String encodeUseBase64(String data){
		Uint8List u8Str = Uint8List.fromList(data.codeUnits);
		String base64Str = base64Encode(u8Str);
		return base64Str;
	}

	static String decodeUseBase64(String data){
		Uint8List u8Str =  base64Decode(data);
		String originStr = String.fromCharCodes(u8Str);
		return originStr;
	}

	///base64url加解密
	static String encodeUseBase64UrlSafe(String data){
		Codec<String, String> stringToBase64Url = utf8.fuse(base64Url);
		String encoded = stringToBase64Url.encode(data);
		return encoded;
	}

	static String decodeUseBase64UrlSafe(String data){
		Codec<String, String> stringToBase64Url = utf8.fuse(base64Url);
		String decoded = stringToBase64Url.decode(data);
		return decoded;
	}
	
//	/// des加密?
//	static Future<String> encodeUseDes(String originText,String keyStr,String iv) async{
//		var encryptBase64 = await FlutterDes.encryptToBase64(originText, keyStr, iv: iv);
//		return encryptBase64;
//	}
//
//	/// des解密?
//	static Future<String> decodeUseDes(String encryptedStr,String keyStr,String iv) async{
//		var originStr = await FlutterDes.decryptFromBase64(encryptedStr, keyStr, iv: iv);
//		return originStr;
//	}
	//endregion
	
	
	//region 非对称加密
	/// md5
	static String encryptUseMd5(String originText) {
		List<int> intList = utf8.encode(originText);
		var result = md5.convert(intList);
		return result.toString();
	}

	/// sha1
	static String encryptUseSha1(String originText){
		List<int> intList = utf8.encode(originText);
		var result = sha1.convert(intList);
		return result.toString();
	}

	/// sha256
	static String encryptUseSha256(String originText){
		List<int> intList = utf8.encode(originText);
		var result = sha256.convert(intList);
		return result.toString();
	}
	
	/// hmac_sha1
	static String encryptUserHmacSha1(String aKey,String originText){
		var key = utf8.encode(aKey);
		var hmacSha1 = Hmac(sha1, key);
		var hmac = hmacSha1.convert(utf8.encode(originText));
		var base64HmacString = Base64Encoder().convert(hmac.bytes);
		return base64HmacString;

		// var bytes = utf8.encode(""""hmac username=$APPID, algorithm="hmac-sha1", headers="x-date", signature=$base64HmacString""");
		// var digest = hmacSha1.convert(bytes);
	}
//endregion

//region 加盐
	static String encryptUseBcrypt(String plaintestPass,String salt){
		return BCrypt.hashpw(plaintestPass,BCrypt.gensalt());
	}

	static bool verifyBcryptPass(String pass,String hashed_pass){
		return BCrypt.checkpw(pass, hashed_pass);
	}
// 	static String gainASalt(){
// 		///logRounds参数的意思是hash几轮,默认就是10轮,为了升级版本后防止默认值被修改
// 		///所以手动设置为10
// 		return BCrypt.gensalt(logRounds: 10);
// 	}

	// static String encryptUseSalt(String plaintestPass,String salt){
	// 	return BCrypt.hashpw(plaintestPass,salt);
	// }
//endregion
}